    //
//  MoreViewController.m
//  Drivers
//
//  Created by Hunk on 10-11-16.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MoreViewController.h"
//#import "SendMailViewController.h"

@implementation MoreViewController

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	// Set title
	[self.navigationController.visibleViewController setTitle:@"关于驾照模拟考试"];
	
	UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg2.png"]];	
	
	self.tableView.backgroundView = imageView;
	
	self.tableView.backgroundColor = [UIColor clearColor];
	
	
	/*
	m_pTableView = [[UITableView alloc] initWithFrame:CGRectMake(30, 50, 260, 360) style:UITableViewStylePlain];
	
	
	m_pTableView.backgroundColor = [UIColor clearColor];
	
	UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg2.png"]];
	
	m_pTableView.backgroundView = imageView;
	
	[imageView release];
	
	
	[self.view addSubview:m_pTableView];
	*/
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 3;
}

#pragma mark -
#pragma mark Set group title 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	NSLog(@"section = %d\n", section);
	
	switch (section) 
	{
		case 0:
		{
			return nil;//@"版权所有";
		}
			break;
		case 1:
		{
			return nil;
		}
			break;
		default:
			break;
	}
	return nil;
}

#pragma mark -
#pragma mark Set number of rows in section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	switch (section)
	{
		case 0:
			return 1;
			break;
		case 1:
			return 1;
			break;
		case 2:
			return 1;
			break;
//		case 3:
//			return 1;
//			break;
		default:
			break;
	}
	return 0;
}

#pragma mark -
#pragma mark Set table cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	switch (indexPath.section)
	{
		case 0:
		{
			return 200.f;
		}
			break;
		case 1:
		{
			return 38.f;
		}
			break;
//		case 2:
//		{
//			return 38.f;
//		}
//			break;
		case 2:
		{
			return 150.f;
		}
			break;
		default:
			break;
	}
	return 0.f;
}

#pragma mark -
#pragma mark Init table cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{	
	UITableViewCell *cell = nil;
	
	switch (indexPath.section)
	{
		case 0:
		{
			static NSString* kSourceCellID = @"FirstCellID";
			cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
			if (cell == nil)
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
			//	cell.alpha = 0.5f;
				cell.backgroundColor = [UIColor clearColor];
				
				// Add imageView
				cell.imageView.image = [UIImage imageNamed:@"imageIcon.png"];
				cell.imageView.opaque = NO;
				
				// Add textLabel
				cell.textLabel.opaque = NO;
				cell.textLabel.textAlignment = UITextAlignmentLeft;
				cell.textLabel.textColor = [UIColor blackColor];
				cell.textLabel.numberOfLines = 5;
				cell.textLabel.highlightedTextColor = [UIColor whiteColor];
				cell.textLabel.font = [UIFont systemFontOfSize:20];	
				//cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:20];
				cell.textLabel.text = @"驾照模拟考试\n\n版权所有@2010 ";
			}
		}
			break;
//		case 1:
//		{
//			static NSString* kSourceCellID = @"ScondCellID";
//			cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
//			if (cell == nil)
//			{
//				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
//				cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//				
//				cell.alpha = 0.5f;
//				cell.textLabel.opaque = NO;
//				cell.textLabel.textAlignment = UITextAlignmentCenter;
//				cell.textLabel.textColor = [UIColor blackColor];
//				cell.textLabel.numberOfLines = 1;
//				cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//				cell.textLabel.font = [UIFont systemFontOfSize:20];
//				cell.textLabel.text = @"更多小巨人软件";
//			}
//			
//		}
//			break;
		case 1:
		{
			static NSString* kSourceCellID = @"ThirdCellID";
			cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
			if (cell == nil)
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
				cell.selectionStyle = UITableViewCellSelectionStyleBlue;
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.backgroundColor = [UIColor clearColor];
				
				cell.alpha = 0.5f;
				cell.textLabel.opaque = NO;
				cell.textLabel.textAlignment = UITextAlignmentCenter;
				cell.textLabel.textColor = [UIColor blackColor];
				cell.textLabel.numberOfLines = 1;
				cell.textLabel.highlightedTextColor = [UIColor whiteColor];
				cell.textLabel.font = [UIFont systemFontOfSize:20];
				cell.textLabel.text = @"撰写邮件给我们";
			}
		}
			break;
		case 2:
		{
			static NSString* kSourceCellID = @"ForthCellID";
			cell = [self.tableView dequeueReusableCellWithIdentifier:kSourceCellID];
			if (cell == nil)
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSourceCellID] autorelease];
				cell.selectionStyle = UITableViewCellSelectionStyleNone;
				cell.backgroundColor = [UIColor clearColor];
				
				//cell.alpha = 0.5f;
				cell.textLabel.opaque = NO;
				cell.textLabel.textAlignment = UITextAlignmentLeft;
				cell.textLabel.textColor = [UIColor blackColor];
				cell.textLabel.numberOfLines = 5;
				cell.textLabel.highlightedTextColor = [UIColor whiteColor];
				cell.textLabel.font = [UIFont systemFontOfSize:20];
				cell.textLabel.text = @"版权申明:\n       本应用属于自主知识产权,侵权必究.\n       望用户好好利用此软件,争取在驾照考试中一次通过.";
			}			
		}
				break;

		default:
			break;
	}
	return cell;
}

#pragma mark -
#pragma mark Table touch event
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	switch (indexPath.section)
	{
		case 0:
		{}
			break;
		case 1:
		{
			[tableView deselectRowAtIndexPath:indexPath animated:YES];
//			
//			SendMailViewController* sendMail = [[SendMailViewController alloc]init];
//			
//			[[UIApplication sharedApplication].keyWindow addSubview:sendMail.view];
//			
//			[sendMail release];
			Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
			if (mailClass != nil)
			{
				// We must always check whether the current device is configured for sending emails
				if ([mailClass canSendMail])
				{
					[self displayComposerSheet];
				}
				else
				{
					[self launchMailAppOnDevice];
				}
			}
			else
			{
				[self launchMailAppOnDevice];
			}			
			
			NSLog(@"撰写邮件\n");
		}
		break;
		default:
			break;
	}
}

#pragma mark -
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayComposerSheet 
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
//    picker.navigationBar.tintColor = [UIColor redColor];
	
	[picker setSubject:@"信息反馈"];
	
	// Set up recipients
	NSArray *toRecipients = [NSArray arrayWithObject:@"driverstestfeedback@gmail.com"]; 
	
	[picker setToRecipients:toRecipients];
	
	// Fill out the email body text
	NSString *emailBody = @"<点击此处进行信息输入>\n\n\n感谢您对<驾照模拟考试>提出宝贵意见和建议\n";
	[picker setMessageBody:emailBody isHTML:NO];
	
	[self presentModalViewController:picker animated:YES];
    [picker release];
}


// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{		
	 [self dismissModalViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{	
	// Recipient(s)
	NSString* strRecipient = @"mailto:driverstestfeedback@gmail.com?subject=驾照模拟考试反馈";
	
	// Mail body
	NSString* strBody = @"&body=<点击此处进行信息输入>\n\n\n感谢您对<驾照模拟考试>提出宝贵意见和建议.";
	
	NSString* strEmail = [NSString stringWithFormat:@"%@%@", strRecipient, strBody];
	strEmail = [strEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strEmail]];
}



#pragma mark -
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
